package com.cens.mall.seckill.interceptor;

import com.cens.common.constant.AuthConstant;
import com.cens.common.vo.MemberEntityVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginIntercepter implements HandlerInterceptor {

    public static final ThreadLocal<MemberEntityVo> loginUser = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean match = new AntPathMatcher().match("/kill", request.getRequestURI());
        if(match){
            MemberEntityVo attribute = (MemberEntityVo) request.getSession().getAttribute(AuthConstant.LOGIN_USER);
            if (attribute != null) {
                loginUser.set( attribute);
                return true;
            }
            request.getSession().setAttribute("msg","请先登录");
            response.sendRedirect("http://auth.censmall.com/login.html");
        }
        return true;
    }
}
